#include "stm32f10x_gpio.h"
#include "stm32f10x_rcc.h"
#include "stm32f10x_spi.h"

#include "lora_spi.h"



/**
 * @beief  LoRa模组的SPI和IO口初始化
 * @param  无
 * @retval 无
 */
void lora_spi_init(void)
{
    SPI_InitTypeDef  SPI_InitStructure;
    GPIO_InitTypeDef GPIO_InitStructure;
	
    SPI_I2S_DeInit(LORA_SPIx);  // LoRa SPI寄存器恢复到默认值
    
	LORA_SPI_APBxClock_FUN ( LORA_SPI_CLK, ENABLE );    /* 使能SPI时钟 */
	
	/* 使能SPI引脚相关的时钟 */
 	LORA_SPI_NSS_APBxClock_FUN ( LORA_SPI_NSS_CLK|LORA_SPI_SCK_CLK|LORA_SPI_MISO_PIN|LORA_SPI_MOSI_PIN, ENABLE );
	
    GPIO_InitStructure.GPIO_Pin = LORA_SPI_NSS_PIN;
	GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
    GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_PP;
    GPIO_Init(LORA_SPI_NSS_PORT, &GPIO_InitStructure);   /* 配置SPI的 NSS引脚，普通IO即可 */
	
    GPIO_InitStructure.GPIO_Pin = LORA_SPI_SCK_PIN;
    GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP;
    GPIO_Init(LORA_SPI_SCK_PORT, &GPIO_InitStructure);  /* 配置SPI的 SCK引脚*/
    
    GPIO_InitStructure.GPIO_Pin = LORA_SPI_MISO_PIN;
    GPIO_Init(LORA_SPI_MISO_PORT, &GPIO_InitStructure); /* 配置SPI的 MISO引脚*/
    
    GPIO_InitStructure.GPIO_Pin = LORA_SPI_MOSI_PIN;
    GPIO_Init(LORA_SPI_MOSI_PORT, &GPIO_InitStructure); /* 配置SPI的 MOSI引脚*/
    
    GPIO_SetBits(LORA_SPI_NSS_PORT, LORA_SPI_NSS_PIN);  /* 停止信号 LORA: NSS引脚高电平*/
    
    SPI_InitStructure.SPI_Direction = SPI_Direction_2Lines_FullDuplex;
    SPI_InitStructure.SPI_Mode = SPI_Mode_Master;
    SPI_InitStructure.SPI_DataSize = SPI_DataSize_8b;
    SPI_InitStructure.SPI_CPOL = SPI_CPOL_Low;
    SPI_InitStructure.SPI_CPHA = SPI_CPHA_1Edge;
    SPI_InitStructure.SPI_NSS = SPI_NSS_Soft;
    SPI_InitStructure.SPI_BaudRatePrescaler = SPI_BaudRatePrescaler_4;
    SPI_InitStructure.SPI_FirstBit = SPI_FirstBit_MSB;
    SPI_InitStructure.SPI_CRCPolynomial = 7;
    SPI_Init(LORA_SPIx, &SPI_InitStructure);   /* SPI 模式配置 */
    
    SPI_Cmd(LORA_SPIx, ENABLE);    /* 使能 SPI  */
    
    LORA_DIO0_APBxClock_FUN( LORA_DIO0_CLK , ENABLE);
    GPIO_InitStructure.GPIO_Pin = LORA_DIO0_PIN;	
    GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IN_FLOATING;   
    //GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz; 
    GPIO_Init(LORA_DIO0_PORT, &GPIO_InitStructure); // DIO0配置为模拟输入
    
    LORA_RST_APBxClock_FUN( LORA_RST_CLK , ENABLE);
    GPIO_InitStructure.GPIO_Pin = LORA_RST_PIN;	
    GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_PP;   
    GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz; 
    GPIO_Init(LORA_RST_PORT, &GPIO_InitStructure); // DIO0配置为模拟输入
}


/**
 * @beief  LoRa SPI发送一个字节
 * @param  发送的数据
 * @retval 收到的数据
 */
uint8_t lora_spi_send_char(uint8_t byte)
{
    uint32_t lora_spi_timeout = LORA_SPI_TIMEOUT_CNT;
    
    while (SPI_I2S_GetFlagStatus(LORA_SPIx , SPI_I2S_FLAG_TXE) == RESET)    /* 等待发送缓冲区为空，TXE事件 */
	{
        if( (lora_spi_timeout--) <= 1) 
            return 0;
    }
    
    SPI_I2S_SendData(LORA_SPIx , byte); /* 写入数据寄存器，把要写入的数据写入发送缓冲区 */
    
    
	lora_spi_timeout = LORA_SPI_TIMEOUT_CNT;
    while (SPI_I2S_GetFlagStatus(LORA_SPIx , SPI_I2S_FLAG_RXNE) == RESET)   /* 等待接收缓冲区非空，RXNE事件 */
    {
        if( (lora_spi_timeout--) <= 1) 
            return 0;
    }
    
    return SPI_I2S_ReceiveData(LORA_SPIx ); /* 读取数据寄存器，获取接收缓冲区数据 */
}